自动映射

  • 全局setting设置
    • autoMappingBehavior默认是PARTIAL,开启自动映射的功能,唯一的要求是列名和javaBean属性名一致
    • 如果autoMappingBehavior设置为null则会取消自动映射
    • 数据库字段命名规范,POJO属性符合驼峰命名法,如A_COLUMN—>aColumn,我们可以开启自动驼峰命名规则映射功能,mapUnderscoreToCamelCase=true
  • 自定义resultMap,实现高级结果集映射

第一个例子

定义接口

1
2
3
public interface EmployeeMapperPlus {
public Employee getEmpById(Integer id);
}

定义映射文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glemontree.mybatis.dao.EmployeeMapperPlus">
<!-- <select id="getEmpById" resultType="com.glemontree.mybatis.bean.Employee">
select * from tbl_employee where id = #{id}
</select> -->
<!-- resultMap:自定义结果集映射规则 -->
<!--
自定义javaBean的封装规则
type:自定义规则的Java类型
id:唯一标识符,方便引用
-->
<resultMap type="com.glemontree.mybatis.bean.Employee" id="MyEmp">
<!--
指定主键列的封装规则
id定义主键底层会有优化
column:指定哪一列
property:指定对应的JavaBean属性
-->
<id column="id" property="id"/>
<!-- result定义普通列封装规则 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列会进行自动封装,推荐写上全部的映射规则 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<select id="getEmpById" resultMap="MyEmp">
select * from tbl_employee where id = #{id}
</select>
</mapper>

测试

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void test06() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
} finally {
openSession.close();
}
}

第二个例子

Employee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.glemontree.mybatis.bean;
import org.apache.ibatis.type.Alias;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + "]";
}
public Employee(Integer id, String lastName, String email, String gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Employee() {
}
}

Department

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.glemontree.mybatis.bean;
public class Department {
private Integer id;
private String departmentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName
+ "]";
}
}

接口

1
public Employee getEmpAndDept(Integer id);

映射文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 联合查询:级联属性封装结果集 -->
<resultMap type="com.glemontree.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
<select id="getEmpAndDept" resultMap="MyDifEmp">
SELECT e.id id, e.last_name last_name, e.gender gender, e.d_id d_id, d.id did, d.dept_name dept_name FROM tbl_employee e, tbl_dept d
WHERE e.d_id = d.id
AND e.id = #{id}
</select>

小结

上面这段程序展示了使用resultMap进行关联查询,在resultMap中使用级联属性封装结果集:

1
2
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>

其实还有另外一种方法可以用来定义关联的单个对象的封装规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 使用association定义关联的单个对象的封装规则 -->
<resultMap type="com.glemontree.mybatis.bean.Employee" id="MyDifEmp2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<!--
association可以指定联合的javaBean对象
property="dept":指定哪个属性时联合的对象
javaType:指定这个属性对象的类型[不能省略]
-->
<association property="dept" javaType="com.glemontree.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>

推荐使用这种方法。